<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>管理员列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}" media="all">
    <script th:src="@{/layuiadmin/layui/layui.js}"></script>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">角色名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="roleName" placeholder="请输入角色名" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <!-- <button class="layui-btn layuiadmin-btn-list" data-type="batchdel">删除</button> -->
                <button class="layui-btn layuiadmin-btn-list" id="add">添加</button>
            </div>
            <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
        </div>
    </div>
</div>

<script type="text/html" id="buttonTpl">
    {{#  if(d.status){ }}
    <button class="layui-btn layui-btn-xs">已发布</button>
    {{#  } else { }}
    <button class="layui-btn layui-btn-primary layui-btn-xs">待修改</button>
    {{#  } }}
</script>
<script type="text/html" id="table-content-list">
    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
</script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['table', 'element'], function () {
        var table = layui.table,
            form = layui.form,
            layer = layui.layer,
            $ = layui.jquery,
            laydate = layui.laydate;

        $(document).off('mousedown', '.layui-table-grid-down').on('mousedown', '.layui-table-grid-down', function (
            event) {
            table._tableTrCurr = $(this).closest('td');
        });
        $(document).off('click', '.layui-table-tips-main [lay-event]').on('click',
            '.layui-table-tips-main [lay-event]',
            function (event) {
                var elem = $(this);
                var tableTrCurr = table._tableTrCurr;
                if (!tableTrCurr) {
                    return;
                }
                var layerIndex = elem.closest('.layui-table-tips').attr('times');
                // 关闭当前这个显示更多的tip
                layer.close(layerIndex);
                table._tableTrCurr.find('[lay-event="' + elem.attr('lay-event') + '"]').first().click();
            });

        table.render({
            elem: '#LAY-app-content-list',
            url: 'list', //模拟接口,
            height: 'full-150',
            method: 'POST',
            cols: [
                [{field: 'id', title: 'id', hide: true},
                    {field: 'roleName', title: '角色名', width: '88%'},
                    {
                        title: '操作',
                        fixed: 'right',
                        width: 140,
                        cellMinWidth: 140,
                        toolbar: '#table-content-list'
                    }
                ]
            ],
            page: true,
            limit: 10,
            limits: [10, 15, 20, 25, 30],
        });


        //监听工具条
        table.on('tool(LAY-app-content-list)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除角色？', function (index) {
                    obj.del();
                    layer.close(index);
                    layer.load(2);
                    $.ajax({
                        url: "del",
                        data: {"id": data.id},
                        type: "POST",
                        success: function (res) {
                            layer.closeAll('loading');
                            if (res.code == 0) {
                                layer.msg("删除成功", {
                                    time: 1000
                                }, function () {
                                });
                            } else {
                                layer.alert(res.message, {icon: 2});
                            }
                        },
                        error: function (xmlHttpRequest) {
                            layer.msg(xmlHttpRequest);
                            layer.closeAll('loading'); //关闭加载层
                        }
                    });

                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    title: '编辑管理员',
                    content: 'edit?id=' + data.id,
                    maxmin: true,
                    area: ['550px', '550px'],
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                        console.log(submit)
                        submit.trigger('click');
                    }
                });
            }
        });

        $("#add").on('click', function () {
            layer.open({
                type: 2,
                title: '添加角色',
                content: 'add',
                maxmin: true,
                area: ['550px', '550px'],
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-add");
                    submit.click();
                }
            });
        });


        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function (data) {
            var field = data.field;
            table.reload('LAY-app-content-list', {
                where: field
            });
        });

    });
</script>
</body>
</html>
